package com.qingke.xml.exerice;

import java.io.IOException;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;
import org.jdom2.xpath.jaxen.JaxenXPathFactory;

public class JDomXpathEvaluator {

	public static void main(String[] args) {
		
		try {
			SAXBuilder builder = new SAXBuilder();
			Document document = builder.build("sample.xml");
			
			Element root = document.getRootElement();
			
			XPathFactory factory = JaxenXPathFactory.instance();
			XPathExpression<Object> xPathExp = factory.compile("//student[./gender='Male']");
			
			List<Object> objectLists = xPathExp.diagnose(root, false).getResult();
			
			for (Object obj: objectLists) {
				Element element = (Element)obj;
				String id = element.getAttributeValue("id");
				System.out.println("id: " + id);
				List<Element> children = element.getChildren();
				for (Element child: children) {
					
					String nodeName = child.getName();
					String nodeValue = child.getText();
					System.out.println("  node: " + nodeName + " = " + nodeValue);
				}
			}
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
